package com.example.demo.controller;

import com.example.demo.entity.data;
import com.example.demo.entity.changeuser;
import com.example.demo.entity.user1;
import com.example.demo.service.UserService;
import com.example.demo.annotation.LocalLock;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import com.example.demo.entity.user;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.concurrent.ExecutionException;

@RestController
@RequestMapping("user")
@CrossOrigin//允许跨域
@Api(tags= "用户相关操作类",value = "用户相关操作类")
public class User{
    @Resource
    UserService UserService;

    @PostMapping("/log")
    @LocalLock(key="log:arg[1]")
    @ApiOperation(value ="登录",notes= "登录成功用户信息，失败返回空")
    public user1 log(@RequestBody data data, HttpServletResponse response) throws ExecutionException, InterruptedException {

        return UserService.log(data,response);
    }

    @PostMapping("/register")
    @ApiOperation(value = "注册",notes = "如果用户名存在返回‘用户名已存在’，否则插入数据，成功返回‘注册成功’，失败返回‘注册失败’")
    public String register(@RequestBody user user)
    {
        return UserService.register(user);
    }

    @PostMapping("/query")
    @ApiOperation(value = "查询",notes = "返回所有用户信息")
    public List query()
    {
        return UserService.query();
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除账号",notes = "输入密码删除账号，成功返回‘删除成功’，失败返回‘密码错误’")
    public String delete(@RequestBody data data)
    {
        return UserService.delete(data);
    }

    @PostMapping("/change")
    @ApiOperation(value = "修改密码",notes = "输入新旧密码，成功返回‘修改成功’，失败返回‘密码错误’")
    public String change(@RequestBody changeuser changeuser)
    {
        return UserService.change(changeuser);
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改信息",notes = "输入所有信息，用户名用作标识不能修改，成功返回‘修改成功’，失败返回‘修改失败’")
    public String update(@RequestBody user user)
    {
        return UserService.update(user);
    }
}
